<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;

class ImgController extends Controller
{
    // 图片上传
    public function upload(Request $request)
    {
        if($_FILES['thumb']['size'] == 0 || $_FILES['thumb']['size']>1024*1024) {
            return show(201, '请上传1M以内的图片');
        }
        $rules = [
            'thumb'=>'required|image'
        ];
        $message = [
            'thumb.*' => '请上传图片'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $type = $request->file('thumb')->getClientOriginalExtension();
        if (!in_array($type,['jpg','jpeg','png'])){
            return show(201,'请上传png|jpg|jpeg格式的图片');
        }
        $user_id = Auth::id();
        if (!$user_id){
            return show(202,'请登陆后在执行该操作',URL::current());
        }

        $path = $request->file('thumb')->storePublicly("user_{$user_id}/info");
        return show(200,'上传成功','/storage/'. $path);
    }

    // 多张图片上传
    public function uploads(Request $request){
        $path = $request->file('img')->storePublicly(md5(time().'hello'));
        return asset('storage/'. $path);
    }




}
